import 'package:flutter/material.dart';
import 'package:flutter_buisiness_2006a/bean/cart_info_bean_result.dart';
import 'package:flutter_buisiness_2006a/pages/cart/cart_bottom.dart';
import 'package:flutter_buisiness_2006a/pages/cart/cart_item.dart';
import 'package:flutter_buisiness_2006a/provider/cart_provider.dart';
import 'package:flutter_buisiness_2006a/provider/counter.dart';
import 'package:provider/provider.dart';

class CartPage extends StatelessWidget {
  const CartPage({Key? key}) : super(key: key);

  Future _getCartInfo(context) async {
    await Provider.of<CartProvider>(context, listen: false).getCartInfo();
    return 'end';
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('购物车'),
      ),
      body: FutureBuilder(
        future: _getCartInfo(context),
        builder: (context, val) {
          if (val.hasData) {
            return Stack(
              children: [
                ListView.builder(
                    itemCount:
                        Provider.of<CartProvider>(context).cartList.length,
                    itemBuilder: (context, index) {
                      return CartItem(
                          item: Provider.of<CartProvider>(context)
                              .cartList[index]);
                    }),
                const Positioned(
                  child: CartBottom(),
                  bottom: 0,
                  left: 0,
                )
              ],
            );
          } else {
            return const Center(
              child: Text('加载中...'),
            );
          }
        },
      ),
    );
  }
}
